﻿using UnityEngine;
using System.Collections;
namespace CommandModel{
	// "Command" 
	public abstract class Command 
	{ 
		// Fields 
		protected Receiver receiver; 

		// Constructors 
		public Command( Receiver receiver ) 
		{ 
			this.receiver = receiver; 
		} 

		// Methods 
		abstract public void Execute(); 
		abstract public void UnExecute();
	} 

	// "ConcreteCommand" 
	class ConcreteCommand : Command 
	{ 
		// Constructors 
		public ConcreteCommand( Receiver receiver ) : 
		base ( receiver ) {} 

		// Methods 
		public override void Execute() 
		{ 
			receiver.Action(); 
		} 
		public override void UnExecute ()
		{
		}
	}	 

	class CalculatorCommand:Command
	{
		char @operator;
		int operand;
		Calculator calculator;
		public CalculatorCommand(Calculator calculator,char @operator,int operand):base(null){
			this.@operator = @operator;
			this.operand = operand;
			this.calculator = calculator;
		}

		public char Operator{
			set{ 
				@operator = value;
			}
		}

		public int Operand{
			set{
				operand = value;
			}
		}

		public override void Execute ()
		{
			calculator.Operation (@operator, operand);
		}

		public override void UnExecute ()
		{
			calculator.Operation( Undo( @operator ), operand ); 
		}

		// Private helper function 
		private char Undo( char @operator ) 
		{ 
			char undo = ' '; 
			switch( @operator ) 
			{ 
			case '+': undo = '-'; break; 
			case '-': undo = '+'; break; 
			case '*': undo = '/'; break; 
			case '/': undo = '*'; break; 
			} 
			return undo; 
		}
	}



}